var _instance;
$(function() {
  //发送验证码
  $("#sendVerificationCode").click(function() {
    if ($(this).hasClass("text-disabled")) {
      return false;
    }
    if (!$("#Phone").valid() | !$("#CaptchaCode").valid()) {
      return false;
    }
    $.showLoading("处理中");
    $.appAjax({
      // 请求地址
      url: "/index.php/Api/login/login",
      data: $("#frmBindPhone").appSerialize(),
      onSuccess: function(c) {
        $.hideLoading();
        if (c.result) {
          $.toast("发送成功");
          var d = 60;
          var b = setInterval(function() {
            d--;
            if (d == 0) {
              $("#sendVerificationCode")
                .text("重新发送")
                .removeClass("text-disabled");
              clearInterval(b);
            } else {
              $("#sendVerificationCode")
                .text("重新发送(" + d + ")")
                .addClass("text-disabled");
            }
          }, 1000);
        } else {
          $.toast(c.message, "cancel");
          a();
        }
      },
      onError: function(b) {
        a();
        $.hideLoading();
        $.toptip("您的网络开了小差，请刷新重试1", 5000, "error");
      }
    });
  });

  //立即认证
  $("#btnCertificationpone").click(function() {
    if (!$("form").valid()) {
      return false;
    }
    //$.showLoading("处理中");
    $.appAjax({
      url: $("#frmBindPhone").attr("action"),
      data: $("#frmBindPhone").appSerialize(),
      onSuccess: function(b) {
        $.toptip(b.msg);

        if(b.type=='1'){
          //跳转到首页
          location.href='/index.php/Api/Index/index';
        }else{
          history.go(0);
        }
        //console.log(b);
        //$.hideLoading();
        //if (b.result) {
        //  $.toast("认证成功", function() {
        //    location.href =
        //      location.href + "&" + parseInt(Math.random() * 1000);
        //  });
        //} else {
        //  $.toast(b.message, "cancel");
        //}
      },
      onError: function(b) {
        console.log(b);

        $.hideLoading();
        $.toptip("您的网络开了小差，请刷新重试2", 5000, "error");
      }
    });
  });

  if ($("#MachineVerify").length) {
    initNECaptcha(
      {
        captchaId: $("#ci").val(),
        element: "#MachineVerify",
        mode: "float",
        width: "100%",
        onVerify: function(b, c) {
          $("#CaptchaCode")
            .val(c.validate)
            .valid();
        }
      },
      function(b) {
        _instance = b;
      },
      function(b) {}
    );
  } else {
    $("#imgCaptchaCode").click(function() {
      $.appAjax({
        // 请求地址
        url: "/index.php/Api/login/login",
        onSuccess: function(b) {
          if (b.result) {
            $("#imgCaptchaCode").attr("src", b.data);
          } else {
            $.toast(b.message, "cancel");
          }
        },
        onError: function(b) {
          $.toptip("您的网络开了小差，请刷新重试3", 5000, "error");
        }
      });
    });
  }
  var a = function() {
    if ($("#MachineVerify").length) {
      _instance.refresh();
    } else {
      $("#imgCaptchaCode").click();
    }
  };
});
